<?php

namespace app\libs\orm;

use \wicked\tools\Inflector;

abstract class Model extends Record
{

    /** @var array */
    protected static $hasMany = [];

    /** @var array */
    protected static $hasOne = [];

    /** @var array */
    protected static $belongsTo = [];

    /**
     * @static
     * @param array $filters
     * @return mixed
     */
    public static function find($filters = [])
    {
        return SimpleORM::get(static::table())->find($filters);
    }

    /**
     * @static
     * @param array $filters
     * @return mixed
     */
    public static function findOne($filters = [])
    {
        return SimpleORM::get(static::table())->findOne($filters);
    }


    /**
     * @param $method
     * @param array $args
     * @return mixed
     */
    /*
    public function __call($method, $args = [])
    {
        // method overriden
        if(method_exists($this, $method))
            return call_user_func_array([$this, $method], $args);
        // has many
        if(array_key_exists($method, static::$hasMany))
            return SimpleORM::get($method)->find([static::$hasMany[$method] => $this->{SimpleORM::convention('primary')}]);
        // has one
        elseif(array_key_exists($method, static::$hasOne))
            return SimpleORM::get($method)->findOne([static::$hasOne[$method] => $this->{SimpleORM::convention('primary')}]);
        // belongs to
        elseif(array_key_exists($method, static::$belongsTo))
            return SimpleORM::get(static::$belongsTo[$method])->findOne([SimpleORM::convention('primary') => $this->{$method}]);
        // what else
        else
            return false;
    }
    */
}
